package cn.org.timespace.plat.action.mc;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.easyframe.action.EFBaseAction;
import cn.org.timespace.ons.util.json.JSONObject;
import cn.org.timespace.ons.util.json.JSONResponseHepler;
import cn.org.timespace.ons.util.pager.Pager;
import cn.org.timespace.ons.util.pager.PagerHelper;

import cn.org.timespace.plat.service.mc.PlatMconfService;
import cn.org.timespace.plat.vo.mc.PlatMconf;

public class McAction  extends EFBaseAction{
	private static Log log = LogFactory.getLog(McAction.class);
	
	private PlatMconfService platMconfService;

	public PlatMconfService getPlatMconfService() {
		return platMconfService;
	}

	public void setPlatMconfService(PlatMconfService platMconfService) {
		this.platMconfService = platMconfService;
	}
	
	public McAction(){
		super(new PlatMconfModel());
	}
	
	PlatMconfModel platMcofModel = (PlatMconfModel)this.getModel();
	
	
	/**
	 * 显示信息终端列表
	 * @return
	 * @throws Exception
	 */
	public String doList() throws Exception{
		Pager p = PagerHelper.getPagerInstance(getRequest());
		List<Object> resList = platMconfService.findByCondition(platMcofModel,p);
		
		if(resList != null && resList.size() == 2){
			p.setTotalCount(((Integer)resList.get(0)));
			p.setData((List)resList.get(1));
		}
		return LIST;
	}
	
	/**
	 * 增加信息终端配置信息
	 * @return
	 * @throws Exception
	 */
	public String doAdd() throws Exception{
		String mcInfoId = getRequest().getParameter("mcInfoId");
		 getRequest().setAttribute("mcInfoId", mcInfoId);
		
		return ADD;
	}
	
	/**
	 * 增加信息终端配置信息
	 * @return
	 * @throws Exception
	 */
	public String doSave() throws Exception{
		HttpServletResponse response =  getResponse();
		
		JSONObject json=new JSONObject();
		try{
			PlatMconf platMconf = platMcofModel.getPlatMconf();
			platMconfService.save(platMconf);
			json.put("result", "0");
			JSONResponseHepler.writeJson(response, json);
		}catch (Exception e)
		{
			log.error("上传文件出错，" + e.toString());
			e.printStackTrace();
			json.put("result", "1");
			JSONResponseHepler.writeJson(response, json);
		}
		
		return null;
	}
	
	/**
	 * 跳转到信息终端配置信息
	 * @return
	 * @throws Exception
	 */
	public String doToUpdate() throws Exception{
		String mcInfoId = getRequest().getParameter("mcInfoId");
		PlatMconf platMconf = platMconfService.findByMcInfoId(mcInfoId);
		platMcofModel.setPlatMconf(platMconf);
		return EDIT;
	}
	
	/**
	 * 修改信息终端配置信息
	 * @return
	 * @throws Exception
	 */
	public String doUpdate() throws Exception{
		HttpServletResponse response =  getResponse();
		
		JSONObject json=new JSONObject();
		try{
			PlatMconf platMconf = platMcofModel.getPlatMconf();
			platMconfService.update(platMconf);
			json.put("result", "0");
			JSONResponseHepler.writeJson(response, json);
		}catch (Exception e)
		{
			log.error("上传文件出错，" + e.toString());
			e.printStackTrace();
			json.put("result", "1");
			JSONResponseHepler.writeJson(response, json);
		}
		
		return null;
	}
}
